package servlet;

import pojo.User;
import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/toupdate")
public class ToUpdateUserServlet extends HttpServlet {
    //@WebServlet与页面链接
    //跳过去，到更新页面

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
//1
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //编码处理  处理汉字传输乱码问题
        req.setCharacterEncoding("utf-8");
        //拿到页面传递的参数
        String sid = req.getParameter("id");
        //写的id 对应  <a href="toupdate?id=${user.id}">修改</a> =前的值
        UserService userService = new UserServiceImpl();
        Integer id=Integer.parseInt(sid);
        //根据id 查询出数据，来唯一查到一条信息
       User user = userService.findById(id);//确保返回值，括号里的值没错，2
       //不能写重定向，要把数据存下来
        //将对象存储在作用域中
        req.setAttribute("user",user);
        //转发，更新到页面
        req.getRequestDispatcher("update.jsp").forward(req,resp);
    }
}
